package io.tec.cloud.algorithm.c01_v20220601;

public class LeetCode_162_852_Offer_069 {

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 1};
        System.out.println(findPeakElement(arr));
    }


    public static int findPeakElement(int[] arr) {


        if (arr.length == 1) {
            return 0;
        }

        if (arr[0] > arr[1]) {
            return 0;
        }

        int length = arr.length;


        if (arr[length - 1] > arr[length - 2]) {
            return length - 1;
        }

        int L = 1;
        int R = length - 2;
        while (L < R) {
            int mid = L + (int) ((R - L) >> 1);
            if (arr[mid] < arr[mid - 1]) {
                R = mid - 1;
            } else if (arr[mid] < arr[mid + 1]) {
                L = mid + 1;
            } else {
                return mid;
            }
        }
        return R;
    }
}
